package com.eazy.lkv2.web.controller.system.count;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.echarts.EnhancedOption;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.data.LineData;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.series.Funnel;
import com.github.abel533.echarts.series.Pie;

/**
 * 饼状图统计，以下代码来源 
 * http://echarts.baidu.com
 * https://git.oschina.net/free/ECharts
 */
@Controller
@RequestMapping("pie")
public class PieCountController extends BaseController {

	@RequestMapping(value = "index")
	public String index() {
		return "count/pie_list";
	}
	
	@RequestMapping(value = "/actDataArray")
	public void actDataArray() {
		EnhancedOption option = new EnhancedOption();
		option.timeline().data("2016-01-01", "2016-02-01", "2016-03-01", "2016-04-01", "2016-05-01",
				new LineData("2016-06-01", "emptyStart6", 8), "2016-07-01", "2016-08-01", "2016-09-01", "2016-10-01",
				"2016-11-01", new LineData("2016-12-01", "star6", 8));
		option.timeline().autoPlay(true);

		Option basic = new Option();
		basic.title().text("浏览器占比变化").subtext("实时数据");
		basic.tooltip().trigger(Trigger.item).formatter("{a} <br/>{b} : {c} ({d}%)");
		basic.legend().data("Chrome", "Firefox", "Safari", "IE9+", "IE8-");
		basic.toolbox().show(true).feature(Tool.mark, Tool.dataView, Tool.restore, Tool.saveAsImage,
				new MagicType(Magic.pie, Magic.funnel).option(new MagicType.Option()
						.funnel(new Funnel().x("25%").width("50%").funnelAlign(X.left).max(1548))));
		int idx = 1;
		Pie pie = getPie(idx++).center("50%", "45%").radius("50%");
		pie.label().normal().show(true).formatter("{b}{c}({d}%)");
		basic.series(pie);
		// 加入
		option.options(basic);
		// 构造11个数据
		Option[] os = new Option[11];
		for (int i = 0; i < os.length; i++) {
			os[i] = new Option().series(getPie(idx++));
		}
		option.options(os);
		renderJson(option.toString());
	}

	/**
	 * 获取饼图数据
	 *
	 * @param idx
	 * @return
	 */
	public Pie getPie(int idx) {
		return new Pie().name("浏览器（数据纯属虚构）").data(new PieData("Chrome", idx * 128 + 80),
				new PieData("Firefox", idx * 64 + 160), new PieData("Safari", idx * 32 + 320),
				new PieData("IE9+", idx * 16 + 640), new PieData("IE8-", idx * 8 + 1280));
	}
}
